/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TextTab.java
 *
 * Created on 1 mars 2010, 10:07:33
 */
package client;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 *
 * @author berthise
 */
public class TextTab extends javax.swing.JPanel {

    private String idTab = new String();
    private Connection connection;
    private ChatView client = null;

    /** Creates new form TextTab */
    public TextTab(ChatView client, Connection connection, String idTab) {
        this.client = client;
        this.idTab = idTab;
        this.connection = connection;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        textAreaPanel = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        CommandPanel = new javax.swing.JPanel();
        textMessage = new javax.swing.JTextField();
        sendButton = new javax.swing.JButton();
        labelMessage = new javax.swing.JLabel();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setName("Form"); // NOI18N
        setLayout(new java.awt.BorderLayout());

        jSplitPane1.setDividerLocation(-3);
        jSplitPane1.setDividerSize(5);
        jSplitPane1.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        jSplitPane1.setContinuousLayout(true);
        jSplitPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jSplitPane1.setDoubleBuffered(true);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        textAreaPanel.setName("textAreaPanel"); // NOI18N

        textArea.setColumns(20);
        textArea.setEditable(false);
        textArea.setRows(5);
        textArea.setName("textArea"); // NOI18N
        textAreaPanel.setViewportView(textArea);

        jSplitPane1.setTopComponent(textAreaPanel);

        CommandPanel.setName("CommandPanel"); // NOI18N
        CommandPanel.setPreferredSize(new java.awt.Dimension(30, 10));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(client.ChatApp.class).getContext().getResourceMap(TextTab.class);
        textMessage.setText(resourceMap.getString("textMessage.text")); // NOI18N
        textMessage.setName("textMessage"); // NOI18N
        textMessage.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textMessageKeyPressed(evt);
            }
        });

        sendButton.setText(resourceMap.getString("sendButton.text")); // NOI18N
        sendButton.setName("sendButton"); // NOI18N
        sendButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                sendButtonMouseClicked(evt);
            }
        });

        labelMessage.setText(resourceMap.getString("labelMessage.text")); // NOI18N
        labelMessage.setName("labelMessage"); // NOI18N

        javax.swing.GroupLayout CommandPanelLayout = new javax.swing.GroupLayout(CommandPanel);
        CommandPanel.setLayout(CommandPanelLayout);
        CommandPanelLayout.setHorizontalGroup(
            CommandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, CommandPanelLayout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(labelMessage, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(textMessage, javax.swing.GroupLayout.PREFERRED_SIZE, 368, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(sendButton)
                .addGap(66, 66, 66))
        );
        CommandPanelLayout.setVerticalGroup(
            CommandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, CommandPanelLayout.createSequentialGroup()
                .addContainerGap(184, Short.MAX_VALUE)
                .addGroup(CommandPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textMessage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(sendButton)
                    .addComponent(labelMessage))
                .addContainerGap())
        );

        jSplitPane1.setRightComponent(CommandPanel);

        add(jSplitPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void sendButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_sendButtonMouseClicked
        sendMessage();
    }//GEN-LAST:event_sendButtonMouseClicked

    private void textMessageKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textMessageKeyPressed
        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_ENTER) {
            sendMessage();
        }
    }//GEN-LAST:event_textMessageKeyPressed

    /*
     * on recupere le message dans le textfield puis l'envois a la connection
     */
    private void sendMessage() {
        // TODO add your handling code here:
        String s = new String();
        if (textMessage.getSelectedText() == null) {
            s = textMessage.getText();
        }
        textMessage.setText("");
        connection.sendMessage(s);
    }

    public void writeMessage(String mess) {

        /**
         * on affiche l'heure, puis le message
         */
        Calendar currentDate = Calendar.getInstance();
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String dateNow = formatter.format(currentDate.getTime());
        textArea.append(dateNow + " : ");
        textArea.append(mess);
        textArea.append("\n");
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel CommandPanel;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel labelMessage;
    private javax.swing.JButton sendButton;
    private javax.swing.JTextArea textArea;
    private javax.swing.JScrollPane textAreaPanel;
    private javax.swing.JTextField textMessage;
    // End of variables declaration//GEN-END:variables

    public void setEnsable(Boolean bool) {
        sendButton.setEnabled(bool);
        textMessage.setEnabled(bool);
    }

    /*
     * getteur et setteur
     */
    public String getIdTab() {
        return idTab;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public void setIdTab(String idTab) {
        this.idTab = idTab;
    }
}
